package com.cloudm.technician.ui.modules.cloudm_box.transfer

import com.cloudm.technician.data.network.CloudmBoxApi
import com.cloudm.technician.ui.base.RxPresenter

/**
 * 作者: wesley
 * 时间: 2017/12/24
 * 描述: 业务逻辑
 */
class TransferPresenter(private val view: TransferContract.View,
                        private val cloudmBoxApi: CloudmBoxApi, private val cloudm: CloudmBoxApi) :
        RxPresenter(), TransferContract.Presenter {

    override fun search(value: String?) {
        if (value == null || value.isNullOrEmpty()) {
            view.showSearchValueEmpMsg()
            return
        }
        view.showLoadingView()
        request(cloudmBoxApi.deviceSearch(value), view, onSuccess = { info, loadMoreInfo ->
            when (info) {
                null -> view.loadSuccess(null, loadMoreInfo)
                else -> view.loadSuccess(arrayListOf(info), loadMoreInfo)
            }
        }, onFailure = { errorMsg ->
            view.loadFailure(errorMsg)
        })
    }


    override fun transfer(deviceId: Int, memberId: Int, channelId: String?, orderNumber: String?,
                          url: String?) {

        view.showTransferDialog()
        request(cloudm.transfer(deviceId, memberId, channelId, orderNumber, url), view,
                onSuccess = { _, _ ->
                    view.hideTransferDialog()
                    view.transferSuccess()
                }, onFailure = { errorMsg ->
            view.hideTransferDialog()
            view.transferFailure(errorMsg)
        })
    }
}



